package com.fenghongzhang.day008;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Pattern;

public class Main2Activity extends AppCompatActivity {


    private EditText username;
    private EditText password;
    private CheckBox cbRemember;
    private Button login;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        cbRemember = (CheckBox) findViewById(R.id.cb_remember);
        login = (Button) findViewById(R.id.login);

        //判断如果issele是true.正面用户已经点击过记住密码的选项了
        SharedPreferences welcome = getSharedPreferences("welcome", MODE_PRIVATE);
        boolean isSele = welcome.getBoolean("isSele", false);
        if (isSele) {
            //取值,并且赋值
            String name = welcome.getString("name", "");
            String pwd = welcome.getString("pwd", "");
            username.setText(name);
            password.setText(pwd);
            cbRemember.setChecked(true);
        }


        //选择以后,才记住
        /**
         * 1,点击登录以后,判断  2,记住密码是否被选中.  2.1 选中以后,存.2.2 没选中以后, 删.
         */
        //1
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //username
                String name = username.getText().toString();
                String pwd = password.getText().toString();

                //加一个正则的判断
                String nameReg = "^[A-Z0-9]{6,12}$";

                //判断数据是否匹配正则
                boolean matches = Pattern.matches(nameReg, name);
                if(matches){
                    Toast.makeText(Main2Activity.this, "ok", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(Main2Activity.this, "buok", Toast.LENGTH_SHORT).show();
                }


                //2
                if(cbRemember.isChecked()){

                    SharedPreferences welcome = getSharedPreferences("welcome", MODE_PRIVATE);
                    SharedPreferences.Editor edit = welcome.edit();
                    edit.putString("name",name);
                    edit.putString("pwd",pwd);
                    edit.putBoolean("isSele",true);
                    edit.commit();
                }else{
                    SharedPreferences welcome1 = getSharedPreferences("welcome", MODE_PRIVATE);
                    SharedPreferences.Editor edit1 = welcome1.edit();
                    //单个移除
//                    edit1.remove("name");
//                    edit1.remove("pwd");
                    //全部移除
//                    edit1.clear();
                    edit1.putBoolean("isSele",false);
                    edit1.commit();
                }
            }
        });



    }
}
